﻿var Contact = {};
core.View.DoAfterView = function (id) {
    $('#assign_href').html("<span class=\"glyphicon glyphicon-share\"></span>&nbsp;分配客户经理");
    //if (Privilege.CREATE > 0) $('#btn_add').css('display', 'inline');
    //if (Privilege.DELETE > 0) $('#btn_delete').css('display', 'inline');
    if (Privilege.ASSIGN > 0) $('#btn_assign').css('display', 'inline');
    if (Privilege.ASSIGN > 0)
        $(core.View.AddButton('btn_assignclient', "分配客服", "glyphicon-share-alt")).unbind().on('click', function () {
            var Checked = $("[name=ListContainer] input[type=checkbox][oid]:checked");
            var num = Checked.length;
            if (num <= 0) return;
            $(core.View.CreateLookupListModal())
                .on('click', function () {
                    $('#myModal').modal('hide');
                    core.View.CreateModal("提示", "<div  style=\"text-align:center\"><p id=\"myModelContent\">  </p>\
                        <div id=\"progress\" class=\"progress \" style=\"opacity:0;height:0px\">\
                            <div  id=\"progress_bar\" class=\"progress-bar progress-bar-striped active  \" role=\"progressbar\" aria-valuemax=\"100\" aria-valuemin=\"0\" style=\"width: 0% ;\">\
                                <span class=\"sr-only\">60%</span>\
                            </div>\
                        </div></div>", "确定", "关闭");
                    Contact.ConfirmAssignUserInfo(0, core.Lookup.GetSelectedOid);
                });

            core.Lookup.BindLookupList("systemuser");

            $('#myModalLabel').html("请选择");

            $('#myModal').modal({ backdrop: 'static', keyboard: false });
        });

//zw   
    $(core.View.AddButton('btn_Companycheck', "校验公司名称", "glyphicon-check")).unbind().on('click', function () {
                core.View.CreateModal("校验公司名称", "<div  style=\"text-align:center\"><p id=\"myModelContent\">  </p>\
                        <div id=\"progress\" class=\"progress \" style=\"opacity:0;height:0px\">\
                            <div  id=\"progress_bar\" class=\"progress-bar progress-bar-striped active  \" role=\"progressbar\" aria-valuemax=\"100\" aria-valuemin=\"0\" style=\"width: 0% ;\">\
                                <span class=\"sr-only\">60%</span>\
                            </div>\
                        </div></div>", "校验", "取消");

        $('#ModalButton1').on('click', function () {                   
                    Contact.Companycheckon(0);
        });
        
        var Checked = $("[name=ListContainer] input[type=checkbox][oid]:checked");
        var num = Checked.length;
        var Oindex1 = $("th[attname='owninguser']").index() - 1;
        var Oindex2 = $("th[attname='tlm_corporatename']").index() - 1;
        var Oindex3 = $("th[attname='tlm_name']").index() - 1;
        var Oval1 = Checked.eq(0).parent('td').siblings('td').eq(Oindex1).html();
        var Oval2 = Checked.eq(0).parent('td').siblings('td').eq(Oindex2).html();
        var Oval3 = Checked.eq(0).parent('td').siblings('td').eq(Oindex3).find('a').text();
        var OId = Checked.eq(0).parent('td').siblings('td').eq(Oindex1).attr("oid");
        var Oid = Checked.eq(0).attr("oid");
        var etn = Checked.eq(0).attr("etn");
        if (num == 0) {
                       return;
        }
        if (num > 1) {
            alert("请选择一个公司");
            return;

        }
        if (num == 1) {
            $('#myModelContent').html("<div style='margin-top:30px'><label for='Companycheckon'>公司名称：</label> <input type='input'id='Companycheckon' style='width:60%' value='" + Oval2 + "'/></br><input type='input'hidden id='Owus' value='" + Oval1 + "'/></br><input type='input'hidden id='OId' value='" + OId + "'/></br><input type='input' hidden id='Oname' value='" + Oval3 + "'/></br><input type='input' hidden id='Companycheckonid' value='" + Oid + "'/></br><input type='input'hidden id='Companycheckonetn' value='" + etn + "'/><div>");
            
        }
         $("#myModal").modal({ backdrop: 'static', keyboard: false }); 
    });
    $("#btn_Companycheck").attr("title", "Companycheck");



    Contact.BindAssignOwningUserButtonClicked();
}
//zw-end


Contact.ConfirmAssignUserInfo = function (index, ClientId, list) {
    if (index == 0) {
        $("#myModal").modal({ backdrop: 'static', keyboard: false });
        list = $("[name=ListContainer] input[type=checkbox][oid]:checked");
    }
    var num = list.length;
    core.showProgressBar(index, num, "修改");
    if (index < num) {
        $.ajax({
            type: "post",
            async: true,
            url: "/ISV/AssignContact/AssignClientID.ashx",
            dataType: "json",
            data: { targetid: list.eq(index).attr("oid"), clientid: ClientId },
            success: function (data, textStatus) {
                if (data.ResultCode == 0) {
                    Contact.ConfirmAssignUserInfo(index + 1, ClientId, list);
                } else {
                    core.ShowProgressError(index, num, data, "修改");
                }
            },
            error: function (xhr, textStatus, errorThrow) {
                $('#myModal').modal('hide');
                Common.AjaxError(xhr, textStatus, errorThrow);
            }
        });
    }
}

Contact.BindAssignOwningUserButtonClicked = function () {
    $('#btn_assign').unbind();
    $('#assign_href').removeAttr('onclick');
    $('#assign_href').on('click', function () {

        var Checked = $("[name=ListContainer] input[type=checkbox][oid]:checked");

        var num = Checked.length;

        if (num <= 0) return;

        $(core.View.CreateLookupListModal()).on('click', function () {
            $('#myModal').modal('hide');
            core.View.CreateModal("提示", "<div  style=\"text-align:center\"><p id=\"myModelContent\">  </p>\
                        <div id=\"progress\" class=\"progress \" style=\"opacity:0;height:0px\">\
                            <div  id=\"progress_bar\" class=\"progress-bar progress-bar-striped active  \" role=\"progressbar\" aria-valuemax=\"100\" aria-valuemin=\"0\" style=\"width: 0% ;\">\
                                <span class=\"sr-only\">60%</span>\
                            </div>\
                        </div></div>", "确定", "关闭");

            Contact.ConfirmAssignOwningUser(0, core.Lookup.GetSelectedOid);
        });
        core.Lookup.BindLookupList("systemuser");
        $('#myModalLabel').html("请选择");
        $('#myModal').modal({ backdrop: 'static', keyboard: false });
    });

}

Contact.ConfirmAssignOwningUser = function (index, clientid, list) {
    if (index == 0) {
        $("#myModal").modal({ backdrop: 'static', keyboard: false });
        list = $("[name=ListContainer] input[type=checkbox][oid]:checked");
    }
    var num = list.length;
    core.showProgressBar(index, num, "修改");
    if (index < num) {
        $.ajax({
            type: "post",
            async: true,
            url: "/ISV/AssignContact/AssignOwningUser.ashx",
            dataType: "json",
            data: { targetid: list.eq(index).attr("oid"), owninguserid: clientid },
            success: function (data, textStatus) {
                if (data.ResultCode == 0) {
                    Contact.ConfirmAssignOwningUser(index + 1, clientid, list);
                } else {
                    core.ShowProgressError(index, num, data, "修改");
                }
            },
            error: function (xhr, textStatus, errorThrow) {
                $('#myModal').modal('hide');
                Common.AjaxError(xhr, textStatus, errorThrow);
            }
        });
    }
}
//zw

Contact.Companycheckon = function (index, checkedItems) {
    
    if (index == 0)
        checkedItems = $("[name=ListContainer] input[type=checkbox][oid]:checked");
    var Otext = $('#Companycheckon').val();
    var Omuser = $('#Owus').val();
    var Oname = $('#Oname').val();
    var Oid = $('#Companycheckonid').val();
    var OId = $('#OId').val();
    var Oetn = $('#Companycheckonetn').val();
            $.ajax({
            type: "post",
            async: true,
            url: "/ISV/AssignContact/Companycheckon.ashx",
            dataType: "json",
            data: { Companyname: Otext },
            success: function (data, textStatus) {
              if (data.ResultCode == 0) {
                    var entityData = {  tlm_corporatename:Otext,
                                        tlm_companycheck: "1",
                                        tlm_contactid: Oid
                    }
                $.ajax({
                        type: "post",
                        url: "/Service/Data/SaveFormData.ashx",
                        dataType: "text",
                        data: { entityname: Oetn, data: JSON.stringify(entityData), id: Oid },
                        success: function (data, textStatus) {
                            core.View.Refresh("ListInnerContainer");
                            $('#myModal').modal('hide');
                            alert("校验成功");
                        },
                        error: function (data) {
                      
                                               }
                    });
                  
                } else {
                        var OentityData = {
                  //      tlm_corporatename: Otext,
                        tlm_companycheck: "2",
                        tlm_contactid: Oid
                    }
                    $.ajax({
                        type: "post",
                        url: "/Service/Data/SaveFormData.ashx",
                        dataType: "text",
                        data: { entityname: Oetn, data: JSON.stringify(OentityData), id: Oid },
                        success: function (data, textStatus) {
                                  
                            var a = confirm("该公司不存在，是否新建客户?");
                            if (a == true) {
                                var postdata = { "tlm_name": { "value": Otext },"owninguser":{"value":Omuser,"oid":OId},"tlm_contactname":{"value":Oname,"oid":Oid}};
                                $.postOpen("/Pages/DataPages/Form/FormPage.aspx?entityname=tlm_account", JSON.stringify(postdata), "_blank"); 

                            }
                            else {
                                return;
                            }
                        },
                        error: function (data) {
                            alert(JSON.stringify(data));
                        }
                    });               
                }
            },
            error: function (xhr, textStatus, errorThrow) {
                $('#myModal').modal('hide');
                Common.AjaxError(xhr, textStatus, errorThrow);
            }
        });
  
}
//zw-end